package com.jet.goods.remote.stock;

import com.jet.core.exception.CustomException;
import com.jet.core.result.ResultCode;
import com.jet.core.result.ResultJson;
import com.jet.goods.remote.stock.dto.StockSpuDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @author dcl
 * @since 2025/2/21 13:48
 */
@Component
@Slf4j
public class GoodsStockRemoteFallback implements FallbackFactory<GoodsStockRemoteFeign> {
    @Override
    public GoodsStockRemoteFeign create(Throwable cause) {
        ResultJson<?> result = new ResultJson<>(ResultCode.SERVICE_ERROR);
        if (cause instanceof CustomException) {
            result = ((CustomException) cause).getResultJson();
        }

        ResultJson<?> finalResult = result;
        return new GoodsStockRemoteFeign() {
            @Override
            public ResultJson<Void> async(long merchantNo, StockSpuDto dto) {
                if (finalResult.getCode() != ResultCode.SUCCESS.getCode()) {
                    return ResultJson.failure(finalResult.getCode(), finalResult.getMsg());
                }
                return ResultJson.ok();
            }

            @Override
            public ResultJson<Void> asyncStatus(long merchantNo, String spuId, int status) {
                if (finalResult.getCode() != ResultCode.SUCCESS.getCode()) {
                    return ResultJson.failure(finalResult.getCode(), finalResult.getMsg());
                }
                return ResultJson.ok();
            }

            @Override
            public ResultJson<Void> deleteBySpuId(long merchantNo, String spuId) {
                if (finalResult.getCode() != ResultCode.SUCCESS.getCode()) {
                    return ResultJson.failure(finalResult.getCode(), finalResult.getMsg());
                }
                return ResultJson.ok();
            }

            @Override
            public ResultJson<Void> deleteBySkuId(String skuId) {
                if (finalResult.getCode() != ResultCode.SUCCESS.getCode()) {
                    return ResultJson.failure(finalResult.getCode(), finalResult.getMsg());
                }
                return ResultJson.ok();
            }
        };
    }
}
